import { IHttpClient } from '@/api/service/IHttpClient'
import { IHttpClientRequestParameters } from '@/api/service/IHttpClientRequestParameters'
import axios,{AxiosRequestConfig,AxiosError,AxiosResponse} from 'axios'
export class HttpClient implements IHttpClient{
    get<T> (parameters: IHttpClientRequestParameters<any>): Promise<T> {
        return new Promise<T>((resolve, reject) => {
            const {url,requiresToken} = parameters;
            const options:AxiosRequestConfig = {
                headers:{}
            }
            axios.get(url,options).then((response:any) =>{
                resolve(response.data as T)
            })
                .catch((response:any) =>{
                    reject(response)
                })
        });
    }

    post<T> (parameters: IHttpClientRequestParameters<any>): Promise<T> {
        return new Promise<T>((resolve, reject) => {
            const {url,payload,requiresToken} = parameters;
            const options:AxiosRequestConfig = {
                headers:{}
            };
            axios.post(url,payload,options)
                .then((response:any) =>{
                    resolve(response.data as T)
                })
                .catch((response:any) =>{
                    reject(response)
                })
        });
    }
}

export const httpClient = new HttpClient();
